package com.tiger.sso;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 
 * @author andy
 *
 */
public class SpringConfigFilter implements Filter {
	private Filter filter;
	
	private Map<String, String> initParameters;

	public SpringConfigFilter() {
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		FilterConfigWritable config = new FilterConfigWritable(filterConfig);
		if(initParameters != null) {
			for(Entry<String, String> e : initParameters.entrySet()) {
				if(config.getInitParameter(e.getKey()) != null) {
					throw new ServletException("repeat config init parameter:" + e.getKey());
				}
				config.setInitParameter(e.getKey(), e.getValue());
			}
		}
		filter.init(config);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		filter.doFilter(request, response, chain);
	}

	@Override
	public void destroy() {
		filter.destroy();
	}

	public Map<String, String> getInitParameters() {
		return initParameters;
	}

	public void setInitParameters(Map<String, String> initParameters) {
		this.initParameters = initParameters;
	}

	public Filter getFilter() {
		return filter;
	}

	public void setFilter(Filter filter) {
		this.filter = filter;
	}
}
